<template>
  <div>
    <el-card>
      <CategorySelect></CategorySelect>
    </el-card>
    <el-card style="margin-top:10px">
      <div v-show="showList">
        <el-button type="primary" size="small" icon="el-icon-plus" :disabled="!category3Id" @click="addAttr">添加属性</el-button>
        <el-table :data="attrList" border>
          <el-table-column label="序号" type="index" width="80" align="center"></el-table-column>
          <el-table-column label="属性名称" prop="attrName" width="150"></el-table-column>
          <el-table-column label="属性值列表">
            <template v-slot="{ row, $index }">
              <el-tag type="success" v-for="attr in row.attrValueList" :key="attr.id">{{ attr.valueName }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template v-slot="{ row, $index }">
              <el-button type="primary" size="mini" icon="el-icon-edit-outline" @click="editAttr(row)"></el-button>
              <el-popconfirm title="确定删除该项吗？" @confirm="deleteAttr(row)">
                <el-button slot="reference" type="danger" size="mini" icon="el-icon-delete"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

      </div>
      <div v-show="!showList">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="属性名">
            <el-input v-model="attrForm.attrName" size="small" placeholder="请输入属性名"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" size="small" icon="el-icon-plus" :disabled="!attrForm.attrName"
          @click="addAttrValue">添加属性值</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
        <!-- 此处table使用的data数据是data中的attrForm，在里面使用$set添加的isEdit属性,使其变成响应式 -->
        <el-table :data="attrForm.attrValueList" style="margin:20px 0;">
          <el-table-column label="序号" type="index" width="80" align="center"></el-table-column>
          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input ref="ipt" v-if="row.isEdit" v-model="row.valueName" placeholder="" size="mini"
                @keyup.enter.native="toDiv(row, $index)" @blur="toDiv(row, $index)"></el-input>
              <div v-else @click="toInput(row, $index)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-button type="danger" size="mini" icon="el-icon-delete"
                @click="deleteAttrValue(row, $index)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" size="small" @click="saveAttr" :disabled="!attrForm.attrName">保存</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import CategorySelect from '@/components/CategorySelect/index.vue'
import { Message } from 'element-ui';
import cloneDeep from 'lodash/cloneDeep'
import { mapActions, mapState } from 'vuex';
export default {
  name: 'Attr',
  data() {
    return {
      showList: true,
      attrForm: {
        attrName: '',
        attrValueList: [],
        categoryLevel: 3,
      },
    }
  },
  components: {
    CategorySelect,
  },
  methods: {
    ...mapActions('attr', ['getAttrInfoList', 'clearAttrList', 'saveAddorEditAttr','deletOneAttr']),
    // 封装获取页面数据的函数
    getAttrInfoData() {
      this.getAttrInfoList({ category1Id: this.category1Id, category2Id: this.category2Id, category3Id: this.category3Id })
    },

    // 点击添加属性按钮添加属性
    addAttr() {
      this.showList = false
      this.attrForm = {
        attrName: '',
        attrValueList: [],
        categoryLevel: 3
      }
    },
    // 点击修改attr
    editAttr(row) {
      this.showList = false
      // 深拷贝row
      this.attrForm = cloneDeep(row)
      // 深拷贝之后操作这个对象不会影响这个row里面的值
      this.attrForm.attrValueList.forEach(item => {
        this.$set(item, 'isEdit', false)
      })
    },
    // 点击列表页面的删除按钮
    async deleteAttr(row) {
      await this.deletOneAttr(row.id)
      this.getAttrInfoData()
    },
    // 点击取消按钮
    cancel() {
      this.showList = true
      this.attrForm.attrName = ''
    },
    // 点击变成编辑模式
    toInput(row) {
      row.isEdit = true
      // 让input获取焦点
      this.$nextTick(() => {
        this.$refs.ipt.focus()
      })
    },
    // 编辑完成失去焦点
    toDiv(row, index) {
      if (!row.valueName.trim()) {//输入内容为空
        this.attrForm.attrValueList.splice(index, 1)
      }
      // 判断属性列表中是否有除了自己本身以外和自己相等的属性，如果有，就删除
      const flag = this.attrForm.attrValueList.some(item => {
        if (item != row) {
          return item.valueName === row.valueName
        }
      })
      if (flag) {
        this.attrForm.attrValueList.splice(index, 1)
      }
      row.isEdit = false
    },
    // 点击添加属性里面的删除按钮
    deleteAttrValue(row, index) {
      this.attrForm.attrValueList.splice(index, 1)
    },
    // 点击添加属性值按钮添加属性值
    addAttrValue() {
      this.attrForm.attrValueList.push({ valueName: '', isEdit: true })
      this.$nextTick(() => {
        this.$refs.ipt.focus()
      })
    },
    // 点击保存按钮发请求保存属性
    async saveAttr() {
      let attrParams = this.attrForm
      attrParams.categoryId = this.category3Id
      // 判断属性值里面有没有空的  当失去焦点和点击保存按钮不知道那个先触发时，我们需要多判断一下将空的属性值删掉
      // 删除isEdit属性
      attrParams.attrValueList = attrParams.attrValueList.filter(item => {
        if (item.valueName.trim()) {//属性值不为空的
          delete item.isEdit
          return true
        }
      })
      try {
        await this.saveAddorEditAttr(attrParams)
        Message.success('保存成功')
        // 显示列表
        this.showList = true
        // 重新获取页面数据
        this.getAttrInfoData()
      } catch (error) {
        Message.error('保存失败')
      }
    }

  },
  computed: {
    ...mapState('category', ['category1Id', 'category2Id', 'category3Id']),
    ...mapState('attr', ['attrList'])
  },
  watch: {
    category3Id(newVal) {
      if (!newVal) {//三级分类未选中状态
        // 清空之前获取到的attrList
        this.clearAttrList()
      } else {
        this.getAttrInfoData()
      }
    }
  }
}
</script>

<style lang="less" scoped></style>

